// TheTend.js for PSPTube
// Made By JK108

function TheTend_CheckURL( url, option )
{
	if (url.match("http://www.thetend.com") == "http://www.thetend.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a TheTend URL
*/
function TheTend_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/so.addVariable\(\'file\',\'(.*?)\'/);
	var video_url = video_url[1];
	return video_url;
}

function TheTend_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 25;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.thetend.com/search_result.php?search_id=" + keyword.replace(/ /g, "+") + "&search_typ=search_videos&x=31&y=18" );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.thetend.com/search_result.php?page=" + num + "&search_type=&search_id=" + keyword.replace(/ /g, "%20") + "&search_key=&sort=" );
	}
	if(contents == null) { return null; /* failed to get page */ }


	var total = contents.match(/ of (.*?)\n/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<div class=\"imagechannel\">");

	if(pos != -1){
		contents = contents.substring(pos + 26, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\<span class\=\"duration\"\>(.*?)\<\/span\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		pos = contents.indexOf("<span class=\"info\">Added:</span>");
		var added = contents.substring(pos + 74, contents.length);
		pos = added.indexOf("\n");
		added = added.substring(0, pos).replace(/  /g, " ");
		pos = contents.indexOf("target=\"_parent\">");
		var user = contents.substring(pos + 40, contents.length);
		pos = user.indexOf("\n");
		user = user.substring(0, pos);
		var description = "Added: " + added + "\nFrom: " + user;
		//Views
		pos = contents.indexOf("<span class=\"info\">Views:</span>");
		var views = contents.substring(pos + 53, contents.length);
		pos = views.indexOf("\n");
		var views = parseInt(views.substring(0, pos).replace(/,/g, ""));
		//Comments
		pos = contents.indexOf("<span class=\"info\">Comments:</span>");
		var comments = contents.substring(pos + 56, contents.length);
		pos = comments.indexOf("\n");
		var comments = parseInt(comments.substring(0, pos).replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var TheTend = new Object();
TheTend.Name          = "TheTend";		/* Service name */
TheTend.Description   = "TheTend";		/* Service description */
TheTend.SearchDesc    = "TheTend";		/* Service search screen description */
TheTend.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
TheTend.CheckURL      = TheTend_CheckURL;	/* CheckURL function */
TheTend.GetURL        = TheTend_GetURL;		/* GetURL function */
TheTend.Search        = TheTend_Search;		/* Search function */

// add TheTend to site list
SiteList.push( TheTend );
